#! /bin/bash

#########################
# usage: iosicon.sh 1024-icon.png
#########################


function print_usage(){

	echo "################################"
	echo "usage: iosicon.sh <icon.png>"
	echo "################################"
}


#check arg
if [[ -z "$1" && $# -ne 1 ]]; then
	print_usage
	exit 1
fi

# prepare
ROOT_DIR=$(pwd)

#check file exist
SOURCE_FILE="${ROOT_DIR}/$1"
if [[ ! -e ${SOURCE_FILE} ]]; then
	echo "source file not exist!"
	exit 2
fi
DEST_DIR="${ROOT_DIR}/ios-icon"
mkdir -p "${DEST_DIR}"
ICON_ARRAY_NAME=(
                 "20.png"   "29.png"
                 "40.png"  
                "50.png"
                 "57.png"
                 "58.png" "60.png" 
                 "72.png"
                 "80.png"
                 "87.png" 
                 "100.png"
                 "114.png"
                 "120.png" "180.png"
                 "216.png"
                 "1024.png" 
                 )

ICON_ARRAY_SIZE=("20"
                "29" 
                 "40" 
                 "50"
                 "57"
                 "58"
                 "60"
                 "72"
                 "80" 
                 "87" 
                 "100"
                 "114"
                 "120" 
                 "180"
                 "216"
                 "1024")


#sips starting
cp "${SOURCE_FILE}" "${DEST_DIR}"
for ((i=0; i<${#ICON_ARRAY_SIZE[@]} ;i++)); do
	size=${ICON_ARRAY_SIZE[i]}
	sips -Z ${size} "${SOURCE_FILE}" --out "${DEST_DIR}/${ICON_ARRAY_NAME[i]}"
done